{include file="header.htm" /}
<body class="bodystyle" style="overflow-y: scroll; cursor: default; -moz-user-select: inherit;">
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page min-hg-c-20">
    <div class="fixed-bar">
        <div class="item-title">
            <a class="back_xin" href="{if condition="'Seckill' == $Request.param.sc && 'index' == $Request.param.sa"}{:url("Weapp/index")}{else /}{:weapp_url("Seckill/Seckill/index")}{/if}" title="返回列表"><i class="iconfont e-fanhui"></i></a>
            <div class="subject">
                <h3>添加活动</h3>
                <h5></h5>
            </div>
        </div>
    </div>
    <form class="form-horizontal" id="post_form" action="{:weapp_url('Seckill/Seckill/add')}" method="post">
        <div class="flexigrid htitx">
            <div class="ncap-form-default">
                <div class="hDiv">
                    <div class="hDivBox">
                        <table cellspacing="0" cellpadding="0" style="width: 100%">
                            <thead>
                            <tr>
                                <th class="sign w10" axis="col0">
                                    <div class="tc"></div>
                                </th>
                                <th abbr="article_title" axis="col3" class="w10">
                                    <div class="tc">基本信息</div>
                                </th>
                                <th abbr="ac_id" axis="col4">
                                    <div class=""></div>
                                </th>
                            </tr>
                            </thead>
                        </table>
                    </div>
                </div>
                <dl class="row">
                    <dt class="tit">
                        <label for="title"><em>*</em>活动名称</label>
                    </dt>
                    <dd class="opt">
                        <input type="text" name="activity_title" id="activity_title" class="input-txt" autocomplete="off">
                        <span class="err"></span>
                        <p class="notic"></p>
                    </dd>
                </dl>
                <dl class="row">
                    <dt class="tit">
                        <label for=""><em>*</em>活动期限</label>
                    </dt>
                    <dd class="opt">
                        <input style="width: 130px !important;" type="text" class="input-txt" id="start_time" name="start_time" placeholder="开始时间" value="" autocomplete="off">
                        ~
                        <input style="width: 130px !important;" type="text" class="input-txt" id="end_time" name="end_time" placeholder="结束时间" value="" autocomplete="off">
                        <span class="err"></span>
                        <p class="notic"></p>
                    </dd>
                </dl>
                <dl class="row">
                    <dt class="tit">
                        <label for=""><em>*</em>活动渠道</label>
                    </dt>
                    <dd class="opt">
                        <label class="curpoin"><input name="activity_channel[]" id="activity_channel" value="weixin" type="checkbox">微信小程序</label>&nbsp;
                        <label class="curpoin"><input name="activity_channel[]" id="activity_channel" value="douyin" type="checkbox">抖音小程序</label>&nbsp;
                        <label class="curpoin"><input name="activity_channel[]" id="activity_channel" value="h5" type="checkbox">H5</label>&nbsp;
                        <label class="curpoin"><input name="activity_channel[]" id="activity_channel" value="pc" type="checkbox">PC</label>&nbsp;
                        <span class="err"></span>
                        <p class="notic"></p>
                    </dd>
                </dl>
                <dl class="row">
                    <dt class="tit">
                        <label for=""><em>*</em>选择商品 </label>
                    </dt>
                    <dd class="opt">
                        <a href="javascript:void(0);" onclick="openArcframe(this);" data-href="{:weapp_url('Seckill/Seckill/ajax_archives_list')}" class="ncap-btn ncap-btn-green"><i class="layui-icon">&#xe608;</i>选择秒杀商品</a>
                        <p class="notic2">提示：秒杀库存不得大于商品库存，用户下单成功即扣除库存；秒杀抢购不支持会员折扣</p>
                        <!-- 已选商品展示页面 -->
                        <div id="showGoods" style="display: none;">{include file="show_goods.htm" /}</div>
                        <input type="hidden" name="aidInput" id="aidInput">
                    </dd>
                </dl>
                <div class="hDiv" id="">
                    <div class="hDivBox">
                        <table cellspacing="0" cellpadding="0" style="width: 100%">
                            <thead>
                            <tr>
                                <th class="sign w10" axis="col0">
                                    <div class="tc"></div>
                                </th>
                                <th abbr="article_title" axis="col3" class="w10">
                                    <div id="anchor-sms" class="tc">规则设置</div>
                                </th>
                                <th abbr="ac_id" axis="col4">
                                    <div class=""></div>
                                </th>
                            </tr>
                            </thead>
                        </table>
                    </div>
                </div>
                {eq name="$dealer_open" value="1"}
                <!-- 需要判断是否开启分销插件判断 -->
                <dl class="row">
                    <dt class="tit">
                        <label for="">参与分销</label>
                    </dt>
                    <dd class="opt">
                        <label class="curpoin"><input name="is_distribution" value="0" type="radio" checked="checked">不参与</label>
                        &nbsp;
                        <label class="curpoin"><input name="is_distribution" value="1" type="radio">参与</label>
                        <span class="err"></span>
                        <p class="notic2">默认不参与分销，分销开启后才能生效</p>
                    </dd>
                </dl>
                {/eq}

                <dl class="row">
                    <dt class="tit">
                        <label>限购次数</label>
                    </dt>
                    <dd class="opt">
                        <label class="curpoin"><input name="is_limit" value="0" type="radio" checked="checked" onclick="change_is_limit(this);">不限制</label>
                        <br>
                        <div class="mt5"></div>
                        <label class="curpoin"><input name="is_limit" value="1" type="radio" onclick="change_is_limit(this);">活动期内每人最多购买 </label>
                        <input type="text" name="limit" placeholder="请输入" id="limit1" class="input-txt" style="width: 80px !important;" disabled="">件
                        <br>
                        <div class="mt5"></div>
                        <label class="curpoin"><input name="is_limit" value="2" type="radio" onclick="change_is_limit(this);">活动期内每人每天最多购买</label>
                        <input type="text" name="limit" placeholder="请输入" id="limit2" class="input-txt" style="width: 80px !important;" disabled>件
                        
                    </dd>
                </dl>
                <div class="bot">
                    <input type="hidden" name="aid" id="aid">
                    <a href="JavaScript:void(0);" onclick="checkForm();" class="ncap-btn-big ncap-btn-green" id="submitBtn">确认提交</a>
                </div>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    function change_is_limit(obj){
        var val = $(obj).val();
        if (0 == val) {
            $('#limit1').attr("disabled","");
            $('#limit2').attr("disabled","");

        }else if (1 == val) {
            $('#limit1').removeAttr("disabled");
            $('#limit2').attr("disabled","");
        }else if (2 == val) {
            $('#limit2').removeAttr("disabled");
            $('#limit1').attr("disabled","");
        }
    }

    layui.use(['form', 'laydate'], function(){
        var form = layui.form,
            laydate = layui.laydate;

        laydate.render({
            elem: '#start_time',
            type: 'datetime'
        });
        laydate.render({
            elem: '#end_time',
            type: 'datetime'
        });

        form.render();
    });

    // 判断输入框是否为空
    function checkForm(){
        if($("#activity_title").val() == ''){
            showErrorMsg('活动名称不能为空！');
            $("#activity_title").focus();
            return false;
        }
        if($("#start_time").val() == ''){
            showErrorMsg('活动开始时间不能为空！');
            $("#start_time").focus();
            return false;
        }
        if($("#end_time").val() == ''){
            showErrorMsg('活动结束时间不能为空！');
            $("#end_time").focus();
            return false;
        }
        var activity_channel_arr = [];
        $("input[id=activity_channel]:checked").each(function(){
            activity_channel_arr.push($(this).val());
        })
        if (0 == activity_channel_arr.length) {
            showErrorMsg('活动渠道至少选择一个！');
            return false;
        }

        var is_limit = $("input[name=is_limit]:checked").val();
        if (0 < is_limit) {
            var limit = $("#limit"+is_limit).val();
            if (1 > limit || '' == limit) {
                showErrorMsg('限制数量不能为空！');
                return false;
            }
        }

        layer_loading('正在处理');
        $.ajax({
            type : 'post',
            url : "{:weapp_url('Seckill/Seckill/add')}",
            data : $('#post_form').serialize(),
            dataType : 'json',
            success : function(res){
                layer.closeAll();
                if(res.code == 1){
                    layer.msg(res.msg, {time: 1000},function () {
                        location.href = res.url;
                    });
                }else{
                    showErrorMsg(res.msg);
                }
            },
            error: function(e){
                layer.closeAll();
                layer.alert(e.responseText, {icon: 5, title:false});
            }
        });
    }

    // 商品参数
    var aidArr = [];
    var aidStr = '';

    // 打开选择商品弹层
    function openArcframe(obj) {
        // 覆盖原先已选的商品ID
        var aidInput = $('#aidInput').val() ? $('#aidInput').val() : '';
        if (aidInput) {
            aidStr = aidInput;
            aidArr = aidStr.split(',');
        }
        // 打开商品弹层
        openFullframe(obj, '选择秒杀商品', '90%', '90%');
    }

    // 选择商品页点击事件
    function selectGoods(obj) {
        // 当前点击商品ID
        var aid = $(obj).attr('data-aid');
        if (aid) {
            // 数组去重
            aidArr = $.unique(aidArr.sort());
            // 判断是否已选择当前点击商品
            var index = $.inArray(aid, aidArr);
            if (-1 === parseInt(index)) {
                // 增加已选
                aidArr.push(aid);
                // 设置点击时间文案和颜色
                $(obj).removeClass('blue').addClass('red').html('已选');
            } else if (-1 !== parseInt(index)) {
                // 去除已选
                aidArr.splice(index, 1);
                // 设置点击时间文案和颜色
                $(obj).removeClass('red').addClass('blue').html('选择');
            }
            aidStr = aidArr.join(',');
        }
    }

    // 提交已选商品 并 加载显示商品
    function submitSelectGoods() {
        layer_loading('正在处理');
        $.ajax({
            url : "{:weapp_url('Seckill/Seckill/showGoods')}",
            data: {aidStr: aidStr, _ajax: 1},
            type: 'post',
            success: function(res) {
                $('#aidInput').val(aidStr);
                $('#showGoods').show().empty().html(res);
                closeSelectGoods(false);
                // if (!res || false === res || null === res || '' === res) {
                //     showErrorMsg('商品异常，刷新重试');
                // } else {
                //     $('#aidInput').val(aidStr);
                //     $('#showGoods').show().empty().html(res);
                //     closeSelectGoods(false);
                // }
            }
        });
    }

    //删除商品
    function del_arc(aid) {
        var aid_old = aidStr.split(',');
        aid_old.forEach(function( val, index ) {
            if ( aid == val ) {
                aid_old.splice(index,1);
            }
        });
        aidStr = aid_old.join(',');
        $('#aidInput').val(aidStr);
        $('#tr_'+aid).remove();
        if ('' == aidStr){
            $("#showGoods").hide();
        }
    }

    // 清空已选商品 并 关闭所有弹框
    function closeSelectGoods(clear) {
        // 清空已选商品
        if (clear) {
            aidArr = [];
            aidStr = '';
        }
        // 关闭所有弹框
        layer.closeAll();
    }

    // 弹出层头部的X关闭回调
    function cancelCallback() {
        closeSelectGoods(true);
    }

</script>
{include file="footer.htm" /}